
package com.craterhill.java.ems.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import com.craterhill.java.ems.common.beans.School;
import com.craterhill.java.ems.dao.SchoolDao;

@Repository
public class SchoolDaoImpl extends AbstractDao<School> implements SchoolDao {

	@Autowired
	public SchoolDaoImpl(final DataSource dataSource) {
		super(dataSource);
	}

	@Override
	public int save(final School school) {
		return super.save(new SchoolPreparedStatementCreator(ACTION_SAVE, school));
	}

	@Override
	public boolean update(final School school) {
		return super.update(new SchoolPreparedStatementCreator(ACTION_UPDATE, school));
	}

	@Override
	public boolean remove(final Integer... schoolIds) {
		return super.batchOperation(DELETE_SCHOOL, new BatchPreparedStatementSetter() {

			@Override
			public void setValues(PreparedStatement preparedStatement, int position) throws SQLException {
				preparedStatement.setInt(1, schoolIds[position]);
			}

			@Override
			public int getBatchSize() {
				return schoolIds.length;
			}
		});
	}

	@Override
	public List<School> findAll() {
		return super.findMultipleObject(FIND_ALL, new SchoolExtractor());
	}

	@Override
	public School find(int schoolId) {
		return super.findSingleObject(FIND_BY_ID, new SchoolExtractor(), schoolId);
	}
}
